package com.bolt.auth.security.common.model;

import com.bolt.common.collection.CollectionUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Administrator on 2019/9/2.
 */
@Getter
@Setter
public class AuthUserDetails implements UserDetails {

    private String userId;

    private String username;

    private String password;

    private Set<String> roles;

    private Set<String> permissions;

    private boolean enabled;

    private boolean superUser;

    private String clientId;

    private String areaCode;

    private String orgId;

    private String orgCode;

    private String authSalt;

    public AuthUserDetails(){

    }

    public AuthUserDetails(String userId, String username, String password, Set <String> roles, Set <String> permissions, boolean enabled) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.roles = roles;
        this.permissions = permissions;
        this.enabled = enabled;
    }

    public AuthUserDetails(String userId, String username, String password, Set <String> roles, Set <String> permissions, boolean enabled, String areaCode,
                           String orgId, String orgCode) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.roles = roles;
        this.permissions = permissions;
        this.enabled = enabled;
        this.areaCode = areaCode;
        this.orgId = orgId;
        this.orgCode = orgCode;
    }

    /***
     * 权限重写
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> collection = new HashSet<>();
        if (!CollectionUtil.isEmpty(roles)) {
            roles.parallelStream().forEach(role -> {
                if (role.startsWith("ROLE_")) {
                    collection.add(new SimpleGrantedAuthority(role));
                } else {
                    collection.add(new SimpleGrantedAuthority("ROLE_" + role));
                }
            });
        }

        if (!CollectionUtil.isEmpty(permissions)) {
            permissions.parallelStream().forEach(per -> {
                collection.add(new SimpleGrantedAuthority(per));
            });
        }

        return collection;
    }



    public Collection<? extends GrantedAuthority> putAll(Collection<GrantedAuthority> collections) {
        Collection<GrantedAuthority> collection = new HashSet<>();

        collection.addAll(collections) ;

        return collection;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

 